﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Collections.ObjectModel;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model;
using Autorent.Desktop.Model.AutorentMngrServiceReference;
using Autorent.Desktop.Model.Services;
using Autorent.Desktop.Modules.Autoes.Services;
using Autorent.Desktop.Modules.Events;
using Autorent.Desktop.Modules.Orders.Services;
using Autorent.Desktop.Modules.Orders.Views;
using Autorent.Desktop.Modules.Offices.Services;
using Autorent.Desktop.Modules.Clients.Views;
using Autorent.Desktop.Modules.Clients.ViewModels;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;

namespace Autorent.Desktop.Modules.Orders.ViewModels
{
    public class CreateAutoOrderViewModel
    {
        #region Public Properties

        public CreateAutoOrderView View { get; set; }

        public AutoOrder AutoOrder { get; set; }

        public ObservableCollection<Office> Offices
        {
            get
            {
                return _officesRepository.GetOffices();
            }
        }

        public ObservableCollection<Auto> Autoes
        {
            get
            {
                try
                {
                    return _autoRepository.GetAutoes();
                }
                catch (AutorentModelException ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return null;
                }
            }
        }

        public ObservableCollection<AutoClass> AutoClasses
        {
            get
            {
                try
                {
                    return _autoClassesRepository.GetAutoClasses();
                }
                catch (AutorentModelException ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return null;
                }
            }
        }

        #endregion

        #region Constructors

        public CreateAutoOrderViewModel(CreateAutoOrderView view, IAutoOrdersRepository autoOrderRepository,
            IOfficesRepository officesRepository, IAutoesRepository autoRepository, IAutoClassesRepository autoClassesRepository,
            IRegionManager regionManager, IUnityContainer container, IEventAggregator eventAggregator)
        {
            View = view;
            view.DataContext = this;
            
            _container = container;
            _autoOrderRepository = autoOrderRepository;
            _officesRepository = officesRepository;
            _autoRepository = autoRepository;
            _autoClassesRepository = autoClassesRepository;
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            
            _eventAggregator.GetEvent<ClientCreatedOrUpdatedEvent>().Subscribe(new System.Action<Client>(ClientCreatedEventHandler));
            _eventAggregator.GetEvent<ViewClosedEvent>().Subscribe(new System.Action<System.Type>(ViewClosedEventHandler));

            this.AutoOrder = new AutoOrder();
        }

        #endregion

        #region Commands

        public ICommand CreateAutoOrder
        {
            get
            {
                return new RelayCommand(param => createAutoOrder());
            }
        }

        public ICommand Cancel
        {
            get
            {
                return new RelayCommand(param => cancel());
            }
        }

        public ICommand CreateClient
        {
            get
            {
                return new RelayCommand(param => createClient());
            }
        }

        #endregion

        #region Private variables

        private readonly IUnityContainer _container;
        private readonly IAutoOrdersRepository _autoOrderRepository;
        private readonly IOfficesRepository _officesRepository;
        private readonly IAutoesRepository _autoRepository;
        private readonly IAutoClassesRepository _autoClassesRepository;
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;

        #endregion

        #region Private Methods

        private void createAutoOrder()
        {
            AutoOrder.Client_ID = AutoOrder.Client.Client_ID;
            AutoOrder.Auto_ID = AutoOrder.Auto.Auto_ID;
            AutoOrder.AutoClass_ID = AutoOrder.AutoClass.AutoClass_ID;
            AutoOrder.AutoOrderRequestDT = DateTime.Now;
            AutoOrder.Office_ID = AutoOrder.Office.Office_ID;
            AutoOrder.Off_Office_ID = AutoOrder.Office1.Office_ID;

            _autoOrderRepository.AddAutoOrder(AutoOrder);

            _eventAggregator.GetEvent<CollectionChangedEvent>().Publish("Orders");

            _regionManager.Regions["DetailsRegion"].Remove(View);

            _eventAggregator.GetEvent<ClientCreatedOrUpdatedEvent>().Unsubscribe(ClientCreatedEventHandler);
            _eventAggregator.GetEvent<ViewClosedEvent>().Unsubscribe(ViewClosedEventHandler);
        }

        private void cancel()
        {
            _regionManager.Regions["DetailsRegion"].Remove(View);

            _eventAggregator.GetEvent<ClientCreatedOrUpdatedEvent>().Unsubscribe(ClientCreatedEventHandler);
            _eventAggregator.GetEvent<ViewClosedEvent>().Unsubscribe(ViewClosedEventHandler);
        }

        private void createClient()
        {
            CreateAutoOrderView createAutoOrderView = (CreateAutoOrderView)_regionManager.Regions["DetailsRegion"].GetView("CreateAutoOrderView");

            if (createAutoOrderView != null)
            {
                createAutoOrderView.Visibility = System.Windows.Visibility.Collapsed;

                CreateClientView view = _container.Resolve<CreateClientViewModel>().View;

                _regionManager.Regions["DetailsRegion"].Add(view, "CreateClientView");
                _regionManager.Regions["DetailsRegion"].Activate(view);
            }
        }

        private void ClientCreatedEventHandler(Client client)
        {
            this.AutoOrder.Client = client;

            CreateAutoOrderView createAutoOrderView = (CreateAutoOrderView)_regionManager.Regions["DetailsRegion"].GetView("CreateAutoOrderView");
            if (createAutoOrderView != null)
            {
                createAutoOrderView.Visibility = Visibility.Visible;
                _regionManager.Regions["DetailsRegion"].Activate(createAutoOrderView);
            }
        }

        private void ViewClosedEventHandler(Type viewType)
        {
            if (viewType == typeof(CreateClientView)) 
            {
                CreateAutoOrderView createAutoOrderView = (CreateAutoOrderView)_regionManager.Regions["DetailsRegion"].GetView("CreateAutoOrderView");
                if (createAutoOrderView != null)
                {
                    createAutoOrderView.Visibility = Visibility.Visible;
                    _regionManager.Regions["DetailsRegion"].Activate(createAutoOrderView);
                }
            }
        }

        #endregion
    }
}
